<?php
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_document.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_user.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_invoice.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_invoice_line.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_inv_status.php";
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_redirect.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';

class api_invoice {

    public static function get_all_invoice_statuses() {
        $inv_status_access = new dao_inv_status();
        return $inv_status_access->get_all();
    }

    public static function get_all_invoices() {
        $invoice_access = new dao_invoice();
        return $invoice_access->get_all();
    }

    public static function get_all_unpaid_invoices() {
        $invoice_access = new dao_invoice();
        return $invoice_access->get_all_unpaid();
    }

    public static function get_all_paid_invoices() {
        $invoice_access = new dao_invoice();
        return $invoice_access->get_all_paid();
    }

    public static function get_all_lines_by_invoice_id($invoice_id) {
        $invoice_line_access = new dao_invoice_line();
        return $invoice_line_access->get_all_by_invoice_id($invoice_id);
    }

    public static function delete_invoice_status($id) {
        $inv_status_access = new dao_inv_status();
        $inv_status_access->delete($id);
        return TRUE;
    }

    public static function delete_invoice($invoice_id) {
        $invoice_access = new dao_invoice();
        $invoice_access->delete($invoice_id);
        return TRUE;
    }

    public static function delete_invoice_line($id) {
        $invoice_line_access = new dao_invoice_line();
        $invoice_line_access->delete($id);
        return TRUE;
    }

    public static function save_invoice_line($invoice_line_id, $invoice_id, $doc_id,
            $money, $quantity) {
        $invoice_line_access = new dao_invoice_line();
        $invoice_line_access->save($invoice_line_id,$invoice_id,$doc_id,$money,$quantity);
        return TRUE;
    }

    public static function save_invoice($invoice_id, $user_id, $created_date, $inv_status_id, $total_money, $total_quantity) {
        $invoice_access = new dao_invoice();
        return $invoice_access->save($invoice_id, $user_id, $created_date, $inv_status_id, $total_money, $total_quantity);
    }

    public static function save_invoice_status($inv_status_id, $inv_status_name, $inv_status_detail) {
        $inv_status_access = new dao_inv_status();
        $inv_status_access->save($inv_status_id,$inv_status_name,$inv_status_detail);
        return TRUE;
    }

    public static function get_invoice_status_by_id($id) {
        $inv_status_access = new dao_inv_status();
        return $inv_status_access->get_by_id($id);
    }

    public static function get_invoice_by_id($id) {
        $invoice_access = new dao_invoice();
        return $invoice_access->get_by_id($id);
    }

    public static function get_invoice_line_by_id($id) {
        $invoice_line_access = new dao_invoice_line();
        return $invoice_line_access->get_by_id($id);
    }

    public static function process_invoice($id) {
        $invoice_access = new dao_invoice();
        $invoice = $invoice_access->get_by_id($id);
        return $invoice_access->save($invoice['invoice_id'], $invoice['user_id'],
                $invoice['created_date'], 2,
                $invoice['total_money'], $invoice['total_quantity']);
    }

    public static function create_invoice($doc_ids, $user_email) {
        $total_money = 0;
        $total_quantity = 0;
        $user = api_user::get_user_by_email($user_email);
        $invoice_id = self::save_invoice(0, $user['user_id'], lib_date::get_now(), 1, $total_money, $total_quantity);
        foreach ($doc_ids as $doc_id) {
            $document = api_document::get_document_by_id($doc_id);
            self::save_invoice_line(0, $invoice_id, $doc_id, $document['doc_price'], 1);
            $total_money += $document['doc_price'];
            $total_quantity += 1;
        }
        self::save_invoice($invoice_id, $user['user_id'], lib_date::get_now(), 1, $total_money, $total_quantity);
    }
}
